package com.ccut;

import com.ccut.Constant.EllipseOrientation;

import static com.ccut.Constant.ShapeType.ELLIPSE;

public class Ellipse extends MyShape {
    private double a;
    private double b;
    private EllipseOrientation orientation;

    public Ellipse() {
        super(ELLIPSE);
    }

    public Ellipse(double a, double b, EllipseOrientation orientation) {
        this();
        this.a = a;
        this.b = b;
        this.orientation = orientation;
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this);
    }

    public EllipseOrientation getOrientation() {
        return orientation;
    }

    public void setOrientation(EllipseOrientation orientation) {
        this.orientation = orientation;
    }

    @Override
    public double calculatePerimeter() {
        // 椭圆周长计算公式（近似）
        return Math.PI * (3 * (a + b) - Math.sqrt((3 * a + b) * (a + 3 * b)));
    }

    @Override
    public double calculateArea() {
        // 椭圆面积计算公式
        return Math.PI * a * b;
    }

    @Override
    public String toString() {
        return "图形的类型:"+ getShapeType().getChineseName() + "\n" +
                "图形的长半轴和短半轴分别为:"+ a + ", " + b + "\n" +
                "图形实轴的位置:"+ (orientation == EllipseOrientation.HORIZONTAL ? "X轴" : "Y轴") + "\n" +
                "图形的周长(近似):"+ String.format("%.2f", getPerimeter()) + "\n" +
                "图形的面积(近似):"+ String.format("%.2f", getArea());
    }

    @Override
    public void showShape() {
        // 创建Swing图形界面来绘制椭圆
        javax.swing.SwingUtilities.invokeLater(() -> {
            // 创建主窗口
            javax.swing.JFrame frame = new javax.swing.JFrame("椭圆绘制");
            frame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);

            // 创建绘图面板
            javax.swing.JPanel panel = new javax.swing.JPanel() {
                @Override
                protected void paintComponent(java.awt.Graphics g) {
                    super.paintComponent(g);
                    java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;

                    // 设置抗锯齿
                    g2d.setRenderingHint(
                            java.awt.RenderingHints.KEY_ANTIALIASING,
                            java.awt.RenderingHints.VALUE_ANTIALIAS_ON
                    );

                    // 获取面板尺寸
                    int width = getWidth();
                    int height = getHeight();

                    // 计算缩放比例
                    double scale = Math.min(width, height) / (2 * Math.max(a, b));
                    int scaledA = (int) (a * scale);
                    int scaledB = (int) (b * scale);

                    // 确保椭圆在可视区域内
                    int displayA = Math.min(scaledA, (int)(width * 0.45));
                    int displayB = Math.min(scaledB, (int)(height * 0.45));
                    int centerX = width / 2;
                    int centerY = height / 2;

                    // 绘制椭圆
                    g2d.setColor(java.awt.Color.BLUE);
                    g2d.drawOval(centerX - displayA, centerY - displayB,
                            displayA * 2, displayB * 2);

                    // 填充椭圆
                    g2d.setColor(new java.awt.Color(0, 0, 255, 50));
                    g2d.fillOval(centerX - displayA, centerY - displayB,
                            displayA * 2, displayB * 2);

                    // 绘制信息文本 - 使用支持中文的字体
                    g2d.setColor(java.awt.Color.BLACK);
                    // 修改为支持中文的字体
                    java.awt.Font font = new java.awt.Font("微软雅黑", java.awt.Font.PLAIN, 12);
                    g2d.setFont(font);
                    g2d.drawString("椭圆 (长半轴: " + String.format("%.2f", Math.max(a, b)) +
                            ", 短半轴: " + String.format("%.2f", Math.min(a, b)) + ")", 20, 20);
                    g2d.drawString("方向: " + (orientation == EllipseOrientation.HORIZONTAL ? "水平" : "垂直"), 20, 40);
                    g2d.drawString("周长: " + String.format("%.2f", getPerimeter()), 20, 60);
                    g2d.drawString("面积: " + String.format("%.2f", getArea()), 20, 80);
                }
            };

            // 添加面板到窗口
            frame.add(panel);
            frame.setSize(500, 500);
            frame.setLocationRelativeTo(null); // 居中显示
            frame.setVisible(true);
        });
    }
}
